﻿using IniHelp;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using SerialHelp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TPMS_ScanComparison
{
    public static class Global
    {
        public static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
        public static ServiceProvider ServiceProvider;
        private static string path2 = System.Environment.CurrentDirectory;
        public static IniFile m_ini = new IniFile(path2 + "\\config.ini");

        public static zSerial PLC = new zSerial();
        public static zSerial 产品 = new zSerial();
        public static zSerial 读码 = new zSerial();
        public static zSerial 电源 = new zSerial();

        public static string curReadStr = "";
        public static string curCurstr = "";


        public static int Number = m_ini.IniReadInt("Number", "Number");

        public static int locket = m_ini.IniReadInt("locket", "locket");
        public static int Medium = m_ini.IniReadInt("Medium", "Medium");
        public static int Outer = m_ini.IniReadInt("Outer", "Outer");
        public static int Weigh = m_ini.IniReadInt("Weigh", "Weigh");
        public static string ProductName = m_ini.IniReadValue("ProductName", "ProductName");
        public static string prefix = m_ini.IniReadValue("prefix", "prefix");
        public static int length = m_ini.IniReadInt("length", "length");


        public static int MediumCount = m_ini.IniReadInt("MediumCount", "MediumCount");
        public static int OuterCount = m_ini.IniReadInt("OuterCount", "OuterCount");

        public static void ReadIniPara()
        {
            try
            {
                PLC.sPort = m_ini.IniReadValue("PLC", "port", PLC.sPort);
                PLC.iRate = m_ini.IniReadInt("PLC", "rate", PLC.iRate);
                PLC.sStopbit = m_ini.IniReadValue("PLC", "stopbit", PLC.sStopbit);
                PLC.iDatabit = m_ini.IniReadInt("PLC", "databit", PLC.iDatabit);
                PLC.sParity = m_ini.IniReadValue("PLC", "parity", PLC.sParity);

                电源.sPort = m_ini.IniReadValue("电源", "port", 电源.sPort);
                电源.iRate = m_ini.IniReadInt("电源", "rate", 电源.iRate);
                电源.sStopbit = m_ini.IniReadValue("电源", "stopbit", 电源.sStopbit);
                电源.iDatabit = m_ini.IniReadInt("电源", "databit", 电源.iDatabit);
                电源.sParity = m_ini.IniReadValue("电源", "parity", 电源.sParity);

                产品.sPort = m_ini.IniReadValue("product", "port", 产品.sPort);
                产品.iRate = m_ini.IniReadInt("product", "rate", 产品.iRate);
                产品.sStopbit = m_ini.IniReadValue("product", "stopbit", 产品.sStopbit);
                产品.iDatabit = m_ini.IniReadInt("product", "databit", 产品.iDatabit);
                产品.sParity = m_ini.IniReadValue("product", "parity", 产品.sParity);

                读码.sPort = m_ini.IniReadValue("读码器", "port", 读码.sPort);
                读码.iRate = m_ini.IniReadInt("读码器", "rate", 读码.iRate);
                读码.sStopbit = m_ini.IniReadValue("读码器", "stopbit", 读码.sStopbit);
                读码.iDatabit = m_ini.IniReadInt("读码器", "databit", 读码.iDatabit);
                读码.sParity = m_ini.IniReadValue("读码器", "parity", 读码.sParity);


            }
            catch (Exception ex)
            {
                Logger.Error("读取ini异常：" + ex.Message);
            }
        }
        public static void WriteplcPara()
        {
            try
            {
                String SectionStr = "PLC";
                m_ini.IniWriteValue(SectionStr, "port", PLC.sPort);
                m_ini.IniWriteValue(SectionStr, "rate", PLC.iRate);
                m_ini.IniWriteValue(SectionStr, "stopbit", PLC.sStopbit);
                m_ini.IniWriteValue(SectionStr, "databit", PLC.iDatabit);
                m_ini.IniWriteValue(SectionStr, "parity", PLC.sParity);
            }
            catch (Exception ex)
            {
                Logger.Error("写入ini异常：" + ex.Message);
            }
        }

        public static void WritePowPara()
        {
            try
            {
                String SectionStr = "电源";
                m_ini.IniWriteValue(SectionStr, "port", 电源.sPort);
                m_ini.IniWriteValue(SectionStr, "rate", 电源.iRate);
                m_ini.IniWriteValue(SectionStr, "stopbit", 电源.sStopbit);
                m_ini.IniWriteValue(SectionStr, "databit", 电源.iDatabit);
                m_ini.IniWriteValue(SectionStr, "parity", 电源.sParity);
            }
            catch (Exception ex)
            {
                Logger.Error("写入ini异常：" + ex.Message);
            }
        }

        public static void WriteProductPara()
        {
            try
            {
                String SectionStr = "product";
                m_ini.IniWriteValue(SectionStr, "port", 产品.sPort);
                m_ini.IniWriteValue(SectionStr, "rate", 产品.iRate);
                m_ini.IniWriteValue(SectionStr, "stopbit", 产品.sStopbit);
                m_ini.IniWriteValue(SectionStr, "databit", 产品.iDatabit);
                m_ini.IniWriteValue(SectionStr, "parity", 产品.sParity);
            }
            catch (Exception ex)
            {
                Logger.Error("写入ini异常：" + ex.Message);
            }
        }

        public static void Write读码器Para()
        {
            try
            {
                String SectionStr = "读码器";
                m_ini.IniWriteValue(SectionStr, "port", 读码.sPort);
                m_ini.IniWriteValue(SectionStr, "rate", 读码.iRate);
                m_ini.IniWriteValue(SectionStr, "stopbit", 读码.sStopbit);
                m_ini.IniWriteValue(SectionStr, "databit", 读码.iDatabit);
                m_ini.IniWriteValue(SectionStr, "parity", 读码.sParity);
            }
            catch (Exception ex)
            {
                Logger.Error("写入ini异常：" + ex.Message);
            }
        }
        public class zSerial
        {
            public plc serial = new plc();
            public string sPort = "COM4"; //com口
            public int iRate = 115200;      //com口速率
            private string _sStopbit = "1";      //停止位
            public int iDatabit = 8;//数据位
            private string _sParity = "None";//校验位
            public string sParity
            {
                get { return _sParity; }
                set
                {
                    _sParity = value;
                    switch (_sParity)
                    {
                        case "None":
                            parity = System.IO.Ports.Parity.None;
                            break;
                        case "Even":
                            parity = System.IO.Ports.Parity.Even;
                            break;
                        case "Odd":
                            parity = System.IO.Ports.Parity.Odd;
                            break;
                        case "Mark":
                            parity = System.IO.Ports.Parity.Mark;
                            break;
                        case "Space":
                            parity = System.IO.Ports.Parity.Space;
                            break;
                        default:
                            parity = System.IO.Ports.Parity.None;
                            break;
                    }
                }
            }
            public System.IO.Ports.Parity parity;
            public System.IO.Ports.StopBits stopcit;
            public string sStopbit
            {
                get { return _sStopbit; }
                set
                {
                    _sStopbit = value;
                    switch (value)
                    {
                        case "1":
                            stopcit = System.IO.Ports.StopBits.One;
                            break;
                        case "1.5":
                            stopcit = System.IO.Ports.StopBits.OnePointFive;
                            break;
                        case "2":
                            stopcit = System.IO.Ports.StopBits.Two;
                            break;
                    }
                }
            }

            public zSerial()
            {
                sStopbit = "1";
                sParity = "None";
                serial.sp = new System.IO.Ports.SerialPort();
            }

            public void 参数赋值(zSerial zs)
            {
                serial.PortName = zs.sPort;
                serial.rate = zs.iRate;
                serial.databits = zs.iDatabit;
                serial.stopBits = zs.stopcit;// global.stopbit;
                serial.parity = zs.parity;
            }
        }
    }
}
